/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Taxi.db.dao;

import Taxi.db.Database;
import Taxi.model.Driver;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author U24022
 */
public class DriverDAO extends BaseDAO {

    public int save(Driver driver) throws SQLException {
        System.out.println("INSERT");
        Statement statement;
        ResultSet rs;
        statement = (Statement) Database.getConnection().createStatement();
        return statement.executeUpdate("INSERT INTO driver(firstName,lastName,address,birthday,license,sss,pagibig,philhealth,startDate,endDate) VALUES( '"
                + driver.getFirstName() + "' , " + "'"
                + driver.getLastName() + "' , " + "'"
                + driver.getAddress() + "' , "
                + driver.getBday() + " , " + "'"
                + driver.getLicense() + "', "
                + +driver.getSss() + " , "
                + +driver.getPagibig() + " , "
                + +driver.getPhilHealth() + " , " 
                + driver.getStartDate() + " , " 
                + driver.getEndDate() + ")");
    }

    public int update(Driver driver) throws SQLException {
        System.out.println("UPDATE");
        Statement statement;
        ResultSet rs;
        statement = (Statement) Database.getConnection().createStatement();
        return statement.executeUpdate("UPDATE driver SET firstName='"
                + driver.getFirstName() + "', lastName='"
                + driver.getLastName() + "', address='"
                + driver.getAddress() + "', birthday="
                + toSQLDateStr(driver.getBday()) + ",license='"
                + driver.getLicense() + "',sss="
                + driver.getSss() + ",pagibig="
                + driver.getPagibig() + ",philhealth="
                + driver.getPhilHealth() + ",startDate="
                + toSQLDateStr(driver.getStartDate()) + ",endDate="
                + toSQLDateStr(driver.getEndDate()) + " WHERE id="
                + driver.getId());
    }

    public int delete(int id) throws SQLException {
        Statement statement;
        statement = (Statement) Database.getConnection().createStatement();
        return statement.executeUpdate("DELETE FROM driver WHERE id =" + id );
    }

// sample UPDATE employee SET emp_num =2 WHERE emp_num =1 AND first_name ='Chan' AND last_name='Howard' AND address='129378 askhdao 1' AND birthday='2000-10-20' AND license='qiweju123734';
    public Driver getDriverByFirstName(String firstname) throws SQLException {
        Statement statement;
        ResultSet rs;
        statement = (Statement) Database.getConnection().createStatement();
        rs = statement.executeQuery("SELECT * FROM driver WHERE firstName='"
                + firstname + "'");
        rs.next();
        return fromRSToDriver(rs);
    }



    public List<Driver> getAllEmployee() throws SQLException {
        Statement statement;
        ResultSet rs;
        statement = (Statement) Database.getConnection().createStatement();
        rs = statement.executeQuery("SELECT * FROM driver");
        List<Driver> employees = new LinkedList<Driver>();
        while (rs.next()) {
            Driver employee = fromRSToDriver(rs);
            employees.add(employee);
        }
        return employees;
    }

    private Driver fromRSToDriver(ResultSet rs) throws SQLException {
        Driver driver = new Driver();
        driver.setId(rs.getInt("id"));
        driver.setFirstName(rs.getString("firstName"));
        driver.setLastName(rs.getString("lastName"));
        driver.setAddress(rs.getString("address"));
        driver.setBday(toDate(rs.getDate("birthday")));
        driver.setLicense(rs.getString("license"));
        driver.setSss(rs.getInt("sss"));
        driver.setPagibig(rs.getInt("pagibig"));
        driver.setPhilHealth(rs.getInt("philhealth"));
        driver.setStartDate(toDate(rs.getDate("start_date")));
        driver.setEndDate(toDate(rs.getDate("end_date")));
        return driver;
    }
}
